module ResourcesHelper
  def check_boxes(form_name, field_name, choices, options)
    object = self.instance_variable_get("@#{form_name}")
    selected = object ? object.send("#{field_name}").to_a : []
    choices = choices.to_a if Hash === choices
    choices.inject([]) do |tags, element|
      if !element.is_a?(String) and element.respond_to?(:first) and element.respond_to?(:last)
        is_selected = ( (selected.respond_to?(:include?) && !selected.is_a?(String) ? selected.include?(element.last) : element.last == selected) )
        if is_selected
          tags << "<input type=\"checkbox\" name=\"#{form_name}[#{field_name}][]\" value=\"#{html_escape(element.last.to_s)}\" selected=\"selected\">#{html_escape(element.first.to_s)}"
        else
          tags << "<input type=\"checkbox\" name=\"#{form_name}[#{field_name}][]\" value=\"#{html_escape(element.last.to_s)}\">#{html_escape(element.first.to_s)}"
        end
      else
        is_selected = ( (selected.respond_to?(:include?) && !selected.is_a?(String) ? selected.include?(element) : element == selected) )
        tags << ((is_selected) ? "input type=\"checkbox\" name=\"#{form_name}[#{field_name}][]\" value=\"#{html_escape(element.to_s)}\" selected=\"selected\">#{html_escape(element.to_s)}" : "input type=\"checkbox\" name=\"#{form_name}[#{field_name}][]\" value=\"#{html_escape(element.to_s)}\">#{html_escape(element.to_s)}")
      end
    end
  end

  def get_tags(resource)
    ResTag.find_all_by_res_id resource.id
  end

  def get_channel(resource)
    section = Section.find(resource.col_id)   
    return section.channel_id
  end

  def destroy_selected(action_name,paramsname,obj_id,url,authenticity_token)
    "<a onclick=\"if (confirm('确认删除资源?')){
       var f = document.createElement('form');
       f.style.display = 'none';
       this.parentNode.appendChild(f);       
       f.method = 'POST';
       var m = document.createElement('input');
       m.setAttribute('type', 'hidden');
       m.setAttribute('name', '_method');
       m.setAttribute('value', '#{action_name}');
       f.appendChild(m);       
       var s = document.createElement('input');
       s.setAttribute('type', 'hidden');
       s.setAttribute('name', 'authenticity_token');       
       s.setAttribute('value', '#{authenticity_token}');       
       f.appendChild(s);
       var a = document.getElementsByTagName('input');
       var n = a.length;       
       ii = 0
       for (var i=0;i<n;i++){
        if((a[i].type == 'checkbox' )&& (a[i].checked == true ) &&
           (a[i].id != 'all')){           
           var s = document.createElement('input');
           s.setAttribute('type', 'hidden');
           s.setAttribute('name', '#{paramsname}');
           s.setAttribute('value', a[i].name.slice(#{obj_id}) );
           f.appendChild(s);           
           ii = ii + 1
         }
       }
       if (ii > 0){ f.action = this.href;  f.submit(); }
     };
    return false;\"  href=\"" + url_for(:controller => url[0], :action => url[1]).to_s+"\">删除选中</a> "
  end

  def move_selected(action_name,section_tag,paramsname,obj_id,url,authenticity_token)
    "<a onclick=\"if (confirm('确认移动资源?')){
       var f = document.createElement('form');
       f.style.display = 'none';
       this.parentNode.appendChild(f);
       f.method = 'POST';
       var m = document.createElement('input');
       m.setAttribute('type', 'hidden');
       m.setAttribute('name', '_method');
       m.setAttribute('value', '#{action_name}');
       f.appendChild(m);
       var s = document.createElement('input');
       s.setAttribute('type', 'hidden');
       s.setAttribute('name', 'authenticity_token');
       s.setAttribute('value', '#{authenticity_token}');
       f.appendChild(s);
       
       var b = document.getElementsByTagName('select');
       var sec = document.createElement('input');
       sec.setAttribute('type', 'hidden');
       sec.setAttribute('name', '#{section_tag}');      
       sec.setAttribute('value',b[2].options[b[2].selectedIndex].value);
       f.appendChild(sec);       
       var a = document.getElementsByTagName('input');
       var n = a.length;
       ii = 0
       for (var i=0;i<n;i++){
        if((a[i].type == 'checkbox' )&& (a[i].checked == true ) &&
           (a[i].id != 'all')){
           var s = document.createElement('input');
           s.setAttribute('type', 'hidden');
           s.setAttribute('name', '#{paramsname}');
           s.setAttribute('value', a[i].name.slice(#{obj_id}) );
           f.appendChild(s);
           ii = ii + 1
         }
       }
       if (ii > 0){ f.action = this.href;  f.submit(); }
     };
    return false;\"  href=\"" + url_for(:controller => url[0], :action => url[1]).to_s+"\">移动资源</a> "
  end

  def copy_selected(action_name,section_tag,paramsname,obj_id,url,authenticity_token)
    "<a onclick=\"if (confirm('确认复制资源?')){
       var f = document.createElement('form');
       f.style.display = 'none';
       this.parentNode.appendChild(f);
       f.method = 'POST';
       var m = document.createElement('input');
       m.setAttribute('type', 'hidden');
       m.setAttribute('name', '_method');
       m.setAttribute('value', '#{action_name}');
       f.appendChild(m);
       var s = document.createElement('input');
       s.setAttribute('type', 'hidden');
       s.setAttribute('name', 'authenticity_token');
       s.setAttribute('value', '#{authenticity_token}');
       f.appendChild(s);

       var b = document.getElementsByTagName('select');
       var sec = document.createElement('input');
       sec.setAttribute('type', 'hidden');
       sec.setAttribute('name', '#{section_tag}');
       sec.setAttribute('value', b[2].options[b[2].selectedIndex].value);
       f.appendChild(sec);

       var a = document.getElementsByTagName('input');
       var n = a.length;
       ii = 0
       for (var i=0;i<n;i++){
        if((a[i].type == 'checkbox' )&& (a[i].checked == true ) &&
           (a[i].id != 'all')){
           var s = document.createElement('input');
           s.setAttribute('type', 'hidden');
           s.setAttribute('name', '#{paramsname}');
           s.setAttribute('value', a[i].name.slice(#{obj_id}) );
           f.appendChild(s);
           ii = ii + 1
         }
       }
       if (ii > 0){ f.action = this.href;  f.submit(); }
     };
    return false;\"  href=\"" + url_for(:controller => url[0], :action => url[1]).to_s+"\">复制资源</a> "
  end

  def add_tag_selected(action_name,c_id,paramsname,obj_id,url,authenticity_token)
    "<a onclick=\"if (confirm('确认增加标签?')){
       var f = document.createElement('form');
       f.style.display = 'none';
       this.parentNode.appendChild(f);
       f.method = 'POST';
       var m = document.createElement('input');
       m.setAttribute('type', 'hidden');
       m.setAttribute('name', '_method');
       m.setAttribute('value', '#{action_name}');
       f.appendChild(m);
       var s = document.createElement('input');
       s.setAttribute('type', 'hidden');
       s.setAttribute('name', 'authenticity_token');
       s.setAttribute('value', '#{authenticity_token}');
       f.appendChild(s);

       var b = document.getElementById('#{c_id}');
       var sec = document.createElement('input');
       sec.setAttribute('type', 'hidden');
       sec.setAttribute('name', b.name);
       sec.setAttribute('value',b.value);
       f.appendChild(sec);       

       var a = document.getElementsByTagName('input');
       var n = a.length;
       ii = 0
       for (var i=0;i<n;i++){
        if((a[i].type == 'checkbox' )&& (a[i].checked == true ) &&
           (a[i].id != 'all')){
           var s = document.createElement('input');
           s.setAttribute('type', 'hidden');
           s.setAttribute('name', '#{paramsname}');
           s.setAttribute('value', a[i].name.slice(#{obj_id}) );
           f.appendChild(s);
           ii = ii + 1
         }
       }
       if (ii > 0){ f.action = this.href;  f.submit(); }
     };
    return false;\"  href=\"" + url_for(:controller => url[0], :action => url[1]).to_s+"\">批量增加标签</a> "
  end

  #推荐到精选频道
  def to_channel(action_name,paramsname,obj_id,url,authenticity_token)
    "<a onclick=\"if (confirm('确认推荐到精选频道?')){
       var f = document.createElement('form');
       f.style.display = 'none';
       this.parentNode.appendChild(f);
       f.method = 'POST';
       var m = document.createElement('input');
       m.setAttribute('type', 'hidden');
       m.setAttribute('name', '_method');
       m.setAttribute('value', '#{action_name}');
       f.appendChild(m);
       var s = document.createElement('input');
       s.setAttribute('type', 'hidden');
       s.setAttribute('name', 'authenticity_token');
       s.setAttribute('value', '#{authenticity_token}');
       f.appendChild(s);
       var a = document.getElementsByTagName('input');
       var n = a.length;
       ii = 0
       for (var i=0;i<n;i++){
        if((a[i].type == 'checkbox' )&& (a[i].checked == true ) &&
           (a[i].id != 'all')){
           var s = document.createElement('input');
           s.setAttribute('type', 'hidden');
           s.setAttribute('name', '#{paramsname}');
           s.setAttribute('value', a[i].name.slice(#{obj_id}) );
           f.appendChild(s);
           ii = ii + 1
         }
       }
       if (ii > 0){ f.action = this.href;  f.submit(); }
     };
    return false;\"  href=\"" + url_for(:controller => url[0], :action => url[1]).to_s+"\">推荐到精选</a> "
  end

  #从精选频道移除
  def move_channel(action_name,paramsname,obj_id,url,authenticity_token)
    "<a onclick=\"if (confirm('确认从精选频道移除?')){
       var f = document.createElement('form');
       f.style.display = 'none';
       this.parentNode.appendChild(f);
       f.method = 'POST';
       var m = document.createElement('input');
       m.setAttribute('type', 'hidden');
       m.setAttribute('name', '_method');
       m.setAttribute('value', '#{action_name}');
       f.appendChild(m);
       var s = document.createElement('input');
       s.setAttribute('type', 'hidden');
       s.setAttribute('name', 'authenticity_token');
       s.setAttribute('value', '#{authenticity_token}');
       f.appendChild(s);
       var a = document.getElementsByTagName('input');
       var n = a.length;
       ii = 0
       for (var i=0;i<n;i++){
        if((a[i].type == 'checkbox' )&& (a[i].checked == true ) &&
           (a[i].id != 'all')){
           var s = document.createElement('input');
           s.setAttribute('type', 'hidden');
           s.setAttribute('name', '#{paramsname}');
           s.setAttribute('value', a[i].name.slice(#{obj_id}) );
           f.appendChild(s);
           ii = ii + 1
         }
       }
       if (ii > 0){ f.action = this.href;  f.submit(); }
     };
    return false;\"  href=\"" + url_for(:controller => url[0], :action => url[1]).to_s+"\">从精选移除</a> "
  end

  #得到图片的真实路径
  def get_img rs
    src_arry = []
    small_path = File.join($upload_path,rs.res_dir,$small)
    src_arry = DataFile.traverse_dir(small_path, src_arry)
    index = 0
    @img_path = nil
    if src_arry.size > 0
      @img_path = src_arry[index].gsub($upload_path, $img_path)
    end
  end
end
